import axios from 'axios'
import { MessageBox, Message } from 'element-ui'
//import store from '@/store'
//import { getToken } from '@/utils/auth'

// 创建一个axios实例
const service = axios.create({
    //真是的url = baseURL + request url
    baseURL: "http://43.139.101.87:8080",
    //请求的超时时间
    timeout: 5000
})

// 请求的拦截器
// service.interceptors.request.use(
//   config => {
//     if (store.getters.token) {
//       // let each request carry token
//       // ['X-Token'] is a custom headers key
//       // please modify it according to the actual situation
//       config.headers['X-Token'] = getToken()
//     }
//     return config
//   },
//   error => {
//     // do something with request error
//     console.log(error) // for debug
//     return Promise.reject(error)
//   }
// )

// 响应的拦截
service.interceptors.response.use(
  //返回响应数据
  response => {
        let res = response.data;
        // 如果是返回的文件
        if (response.config.responseType === 'blob') {
            return res
        }
        // 兼容服务端返回的字符串数据
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }

        if (res.code !== 200){
            Message({
                message: res.message || 'Error',
                type: 'error',
                duration: 2 * 1000
            })
        }
        return res;
    },
  error => {
    console.log('err' + error) // for debug
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
